#!/bin/bash
set -e

host="127.0.0.1"
port=3306
user="root"
password=""
sql=""
ssl_key=""
ssl_cert=""
quiet=0

while [[ ${1} ]]; do
	case "${1}" in
	--host)
		host=${2}
		shift
		;;
	--port)
		port=${2}
		shift
		;;
	--user)
		user=${2}
		shift
		;;
	--password)
		password=${2}
		shift
		;;
	--ssl-key)
		ssl_key=${2}
		shift
		;;
	--ssl-cert)
		ssl_cert=${2}
		shift
		;;
	--quiet)
		quiet=1
		;;
	*)
		sql="$sql ${1}"
		;;
	esac

	if ! shift; then
		echo 'Missing parameter argument.' >&2
		exit 1
	fi
done

cmd="mysql -u${user} -h${host} -P${port}"
if [[ -n ${password} ]]; then
	cmd="${cmd} -p${password}"
fi
if [[ -n ${ssl_key} ]]; then
	cmd="${cmd} --ssl-key=${ssl_key}"
fi
if [[ -n ${ssl_cert} ]]; then
	cmd="${cmd} --ssl-cert=${ssl_cert}"
fi
cmd="${cmd} --default-character-set utf8mb4 -e \"${sql}\""

if [[ ${quiet} -ne 1 ]]; then
	echo "will execute: $cmd" >&2
fi
set -f
eval $cmd
set +f
